﻿using GPM;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LF3.Controllers;
using LF3.Moduls;
using CP.Utility;
using LF3Proj.Properties;

namespace LF3Proj
{
    public partial class FrmLFMonitor : BaseForm
    {
        public FrmLFMonitor()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.lblHeatID.Text))
            {
                ExMessageBox.Show("当前炉次正在生产，不能通报新炉次", AppSetter.SoftName);
                return;
            }
            FrmHeatAnnounce form = new FrmHeatAnnounce();
            form.ShowDialog();
            DisplayAllHeatInfo();
        }

        private void FrmLFMonitor_Load(object sender, EventArgs e)
        {
            DisplayAllHeatInfo();
        }

        private void DisplayAllHeatInfo()
        {
            try
            {
                DisplayCurrentHeatInfo();
                DisplayHeatEvent();
                DisplaySample();
                DisplayMatConsume();
                DisplayMeasuring();
                SetHeatPicture();
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message, AppSetter.SoftName);
            }
        }

        private void SetHeatPicture()
        {
            if (string.IsNullOrEmpty(this.lblHeatID.Text))
                this.picOnOff.Image = Resources.off;
            else
                this.picOnOff.Image = Resources.on;
        }

        private void FrmLFMonitor_Shown(object sender, EventArgs e)
        {
            this.btnAdd.Focus();
        }

        private void DisplayCurrentHeatInfo()
        {
            LF_CurrentHeatController ctl = new LF_CurrentHeatController();
            V_Schedule_Tapping heat = ctl.GetCurrentHeatInfo();

            int ccid = string.IsNullOrEmpty(heat.casterno) ? 0 : Convert.ToInt16(heat.casterno.Substring(1, 1));
            Lf_Cc_Status_Info cc = ctl.GetCCInfo(ccid);

            lblHeatID.Text = heat.heatid;
            lblPlanNo.Text = heat.bofscheduleid.ToString();
            lblSteelGrade.Text = heat.steelgrade;

            lblLadleID.Text = heat.ladleid;
            lblLadleLife.Text = heat.ladlelife.ToString();
            lblLadleStatus.Text = heat.ladlestatus.ToString();
            lblEmptyWet.Text = heat.emptyladlewgt.ToString();

            lblBofID.Text = heat.bofid;
            lblSteelWet.Text = heat.steelwet.ToString();
            lblTapTemp.Text = heat.taptemp.ToString();

            lblHeatDual.Text = heat.melttime.ToString();
            lblArConsume.Text = heat.arconsume.ToString();
            lblPowerConsume.Text = heat.lhfpower.ToString();
            lblPowerTime.Text = heat.powertime.ToString();
            lblArTime.Text = heat.lhfsoftartime.ToString();
            lblPreTemp.Text = heat.pretemp.ToString();

            lblCCNo.Text = cc.ccid.ToString();
            lblLadleWet.Text = cc.remldlstlwgt.ToString();
            lblTundishWet.Text = cc.remtundstlwgt.ToString();
        }

        private void DisplayHeatEvent()
        {
            //if (string.IsNullOrEmpty(this.lblHeatID.Text))
            //    return;
            LF_CurrentHeatController ctl = new LF_CurrentHeatController();
            this.gcEvent.DataSource = ctl.GetHeatEvent(this.lblHeatID.Text);
        }

        private void DisplaySample()
        {
            //if (string.IsNullOrEmpty(this.lblHeatID.Text))
            //    return;
            Lf_Steel_Sample_AnalysisController ctl = new Lf_Steel_Sample_AnalysisController();
            this.gcLFSample.DataSource = ctl.GetLFSample(this.lblHeatID.Text);
            this.gcBofSample.DataSource = ctl.GetBofSample(this.lblHeatID.Text);
        }

        private void DisplayMatConsume()
        {
            //if (string.IsNullOrEmpty(this.lblHeatID.Text))
            //    return;
            Lf_Material_ConsumeController ctl = new Lf_Material_ConsumeController();
            this.gcMatConsume.DataSource = ctl.GetMatConsume(this.lblHeatID.Text);
            this.gcWs.DataSource = ctl.GetWSConsume(this.lblHeatID.Text);
        }

        private void DisplayMeasuring()
        {
            //if (string.IsNullOrEmpty(this.lblHeatID.Text))
            //    return;
            Lf_Measuring_InfoController ctl = new Lf_Measuring_InfoController();
            this.gcTemp.DataSource = ctl.GetMeasureInfo(this.lblHeatID.Text);
        }

        private void btnHeatCancel_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.lblPlanNo.Text))
            {
                ExMessageBox.Show("当前没有正在生产的炉次，不能通报取消", AppSetter.SoftName);
                return;
            }
            if (ExMessageBox.Confirm("确定要炉次取消通报吗？", AppSetter.SoftName) != DialogResult.Yes)
                return;

            LF_SchedulerController ctl = new LF_SchedulerController();
            string msg = ctl.HeatCancelAnnounce(Convert.ToInt32(this.lblPlanNo.Text));

            if (string.IsNullOrEmpty(msg))
            {
                ExMessageBox.Show("提示信息：炉次取消通报成功！", AppSetter.SoftName);
                LogHelper.Log("炉次监控画面", "删除", "取消通报", this.lblPlanNo.Text, this.lblHeatID.Text);
            }
            else
                ExMessageBox.Show("提示信息：炉次取消通报失败！错误信息：" + msg, AppSetter.SoftName);
            DisplayAllHeatInfo();
        }

        private void btnHeatEnd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.lblPlanNo.Text))
            {
                ExMessageBox.Show("当前没有正在生产的炉次，不能炉次结束", AppSetter.SoftName);
                return;
            }
            if (ExMessageBox.Confirm("确定要炉次炉次结束吗？", AppSetter.SoftName) != DialogResult.Yes)
                return;

            LF_SchedulerController ctl = new LF_SchedulerController();
            string msg = ctl.HeatEnd(Convert.ToInt32(this.lblPlanNo.Text));

            if (string.IsNullOrEmpty(msg))
            {
                ExMessageBox.Show("提示信息：炉次结束成功！", AppSetter.SoftName);
                LogHelper.Log("炉次监控画面", "删除", "炉次结束", this.lblPlanNo.Text, this.lblHeatID.Text);
            }
            else
                ExMessageBox.Show("提示信息：炉次结束失败！错误信息：" + msg, AppSetter.SoftName);
            DisplayAllHeatInfo();
        }

        private void btnSample_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.lblPlanNo.Text))
            {
                ExMessageBox.Show("当前没有正在生产的炉次，不能取样", AppSetter.SoftName);
                return;
            }
            FrmSampleCount form = new FrmSampleCount(this.lblHeatID.Text);
            form.Location = new Point(780, 570);
            form.ShowDialog();
        }

        private void btnHeatSample_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.lblPlanNo.Text))
            {
                ExMessageBox.Show("当前没有正在生产的炉次，没有化验信息", AppSetter.SoftName);
                return;
            }
            FrmCurrentSample form = new FrmCurrentSample(this.lblHeatID.Text, this.lblSteelGrade.Text);
            form.ShowDialog();
        }

        private void btnMatInput_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.lblPlanNo.Text))
            {
                ExMessageBox.Show("当前没有正在生产的炉次，没有投料信息", AppSetter.SoftName);
                return;
            }
            FrmCurrentMaterialConsume form = new FrmCurrentMaterialConsume(this.lblHeatID.Text, Convert.ToInt64(this.lblPlanNo.Text));
            form.ShowDialog();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DisplayAllHeatInfo();
        }

        private void btnSetWire_Click(object sender, EventArgs e)
        {
            FrmWireConfig frm = new FrmWireConfig();
            frm.ShowDialog();
        }
    }
}
